#include <json/json.h>
#include <fstream>
#include <iostream>
using namespace Json;
using namespace std;

void writeJson();
void readJson();

// int main()
// {
//     writeJson();
//     readJson();
// }

void writeJson()
{
    Value root;

    root.append(12);	
    root.append(12.34);
    root.append(true);
    root.append("tom");


    Value subArray;
    subArray.append("jack");
    subArray.append("ace");
    subArray.append("robin");
    root.append(subArray);


    Value subObj;
    subObj["sex"] = "woman";  
    subObj["girlfriend"] = "lucy";
    root.append(subObj);


    #if 1
    	string str = root.toStyledString();
    #else
        FastWriter f;
        string str = f.write(root);
    #endif

    ofstream ofs("test.json");
    ofs << str;
    ofs.close();
}

void readJson()
{

    ifstream ifs("test.json");

    Value root;
    Reader r;
    r.parse(ifs, root);

    if (root.isArray())
    {

        for (int i = 0; i < root.size(); ++i)
            {

                Value item = root[i];

                if (item.isString())
                {
                    cout << item.asString() << ", ";
                }
                else if (item.isInt())
                {
                    cout << item.asInt() << ", ";
                }
                else if (item.isBool())
                {
                    cout << item.asBool() << ", ";
                }
                else if (item.isDouble())
                {
                    cout << item.asFloat() << ", ";
                }
                else if (item.isArray())
                {
                    for (int j = 0; j < item.size(); ++j)
                        {
                            cout << item[j].asString() << ", ";
                        }
                }
                else if (item.isObject())
                {
                    Value::Members keys = item.getMemberNames();
                    for (int k = 0; k < keys.size(); ++k)
                        {
                            cout << keys.at(k) << ":" << item[keys[k]] << ", ";
                        }
                }

            }
        cout << endl;
    }
}